package com.xyy.kingdeejdy.task;


import com.xyy.kingdeejdy.pojo.pop.PopConfig;
import com.xyy.kingdeejdy.service.pop.PopToErpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
@Slf4j
public class SaticScheduleTask {
    @Autowired
    private PopToErpService popToErpService;

    private List<PopConfig> popConfigList = new ArrayList<>();

    @PostConstruct
    public void init() {
        popConfigList.add(PopConfig.builder().orgId("SH10005052").appId("1370305312904445954").appSecret("1370305312912838749").build());
        popConfigList.add(PopConfig.builder().orgId("SH10033791").appId("1646798106014003294").appSecret("1646798106018197569").build());
        popConfigList.add(PopConfig.builder().orgId("SH10059479").appId("1745632715308290094").appSecret("1745632715308290095").build());
        popConfigList.add(PopConfig.builder().orgId("SH10029164").appId("1640889378312032346").appSecret("1640889378316234830").build());
    }

    //3.添加定时任务

    /**
     * 商品库存同步   一分钟执行一次
     */
    @Scheduled(cron = "0 0/10 * * * ? ")
    private void ProductStock() {
        for (PopConfig popConfig : popConfigList) {
            log.info("商品库存同步开始,orgId:{}", popConfig.getOrgId());
            popToErpService.realStockPush(popConfig);
            log.info("商品库存同步结束,orgId:{}", popConfig.getOrgId());
        }
    }

    /**
     * 商品批次同步   一分钟执行一次
     */
    @Scheduled(cron = "0 0/30 * * * ? ")
    private void ProductBatch() {
        for (PopConfig popConfig : popConfigList) {
            log.info("商品批次同步开始,orgId:{}", popConfig.getOrgId());
            popToErpService.ProductStockPush(popConfig);
            log.info("商品批次同步结束,orgId:{}", popConfig.getOrgId());
        }
    }

    /**
     * 商品价格同步   一分钟执行一次
     */
    @Scheduled(cron = "0 0/10 * * * ? ")
    private void ProductPrice() {
        for (PopConfig popConfig : popConfigList) {
            log.info("商品价格同步开始,orgId:{}", popConfig.getOrgId());
            popToErpService.ProductPricePush(popConfig);
            log.info("商品价格同步结束,orgId:{}", popConfig.getOrgId());
        }
    }

    /**
     * 订单同步
     */
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void orderPull() {
        for (PopConfig popConfig : popConfigList) {
            log.info("订单同步开始,orgId:{}", popConfig.getOrgId());
            popToErpService.orderInfoPull(popConfig);
            log.info("订单同步结束,orgId:{}", popConfig.getOrgId());
        }
    }

    /**
     * 客户信息上报
     */
    @Scheduled(cron = "0 0/10 * * * ? ")
    public void customerToErp() {
        for (PopConfig popConfig : popConfigList) {
            log.info("客户信息上报开始,orgId:{}", popConfig.getOrgId());
            popToErpService.customerInfoPush(popConfig);
            log.info("客户信息上报结束,orgId:{}", popConfig.getOrgId());
        }
    }

    /**
     * 客户导入
     */
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void customerToJd() {
        for (PopConfig popConfig : popConfigList) {
            log.info("客户导入开始,orgId:{}", popConfig.getOrgId());
            popToErpService.customerInfoPull(popConfig);
            log.info("客户导入结束,orgId:{}", popConfig.getOrgId());
        }
    }

    /**
     * 出库明细
     */
    @Scheduled(cron = "0/30 * * * * ? ")
    public void orderDetail() {
        for (PopConfig popConfig : popConfigList) {
            log.info("出库明细同步开始,orgId:{}", popConfig.getOrgId());
            popToErpService.orderDeliver(popConfig);
            log.info("出库明细同步结束,orgId:{}", popConfig.getOrgId());
        }
    }

}